//
//  GDMainViewController.swift
//  weibo
//
//  Created by user on 15/11/23.
//  Copyright © 2015年 user. All rights reserved.
//

import UIKit

class GDMainViewController: UITabBarController {
    
    
    func composeClick(){
        print("呵呵")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        /**
        适用于字体图片一样颜色的
        */
        tabBar.tintColor = UIColor.orangeColor()
        //首页
        let HomeVc = GDHomeViewController()
        addChildViewController(HomeVc, title: "首页", imageName: "tabbar_home")
        
        //消息
        let MessageVc = GDMessageViewController()
        addChildViewController(MessageVc, title: "消息", imageName: "tabbar_message_center")
        
        //占位控制器
        addChildViewController(UIViewController())
        
        //发现
        let DiscoverVc = GDDiscoverViewController()
        addChildViewController(DiscoverVc, title: "发现", imageName: "tabbar_discover")
        
        //我
        let ProgileVc = GDProgileViewController()
        addChildViewController(ProgileVc, title: "我", imageName: "tabbar_profile")
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //设置加号按钮的frame
        let width = tabBar.bounds.size.width / 5
        let frame = CGRect(x: 2 * width - 5, y: 0, width: width + 10, height: tabBar.bounds.height)
        composeButton.frame = frame
        
    }
    
//    private 表示只有本类或当前文件的其他类可以访问
    private func addChildViewController(controller: UIViewController, title: String, imageName: String) {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        /**
        //        *  适用于字体图片不一样的时候
        //        */
//        let highlightedImageName = imageName + "_highlighed"
//        //不要系统渲染
//        let highlightedImage = UIImage(named: highlightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        
//
//        //设置高亮图片
//        controller.tabBarItem.selectedImage = highlightedImage
//        //设置标题颜色
//        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Highlighted)
        
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    
    //懒加载
    //加号按钮
        lazy var composeButton: UIButton = {
            let button = UIButton()
            // 设置参数
            // 设置按钮背景图片
            button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)

        
            // 设置按钮图片
            button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
            button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)

        // 添加到tabbar里面
        self.tabBar.addSubview(button)
        //添加点击事件
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

    

}
